package com.liu.susu.controller;

import com.alibaba.fastjson.JSONObject;
import com.liu.susu.dao.DogDao;
import com.liu.susu.pojo.Dog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/dog/dao1")
public class DogDaoController {

    @Autowired
    private DogDao dogDao;

    /**
     * 查询所有
     * @return
     */
    @ResponseBody
    @RequestMapping("/all")
    public String testFindAll(){
        List<Dog> dogList = dogDao.findAll();
        System.out.println(JSONObject.toJSONString(dogList));
        return JSONObject.toJSONString(dogList);
    }
    /**
     * 查询所有 并 根据dogAge字段进行排序
     * @return
     */
    @ResponseBody
    @RequestMapping("/all2")
    public List<Dog> testFindAll2(){
//        List<Dog> dogList = dogDao.findAll(Sort.by("dogAge"));//默认升序
        List<Dog> dogList = dogDao.findAll(Sort.by("dogAge").descending());
        return dogList;
    }
    /**
     * 查询所有 并 根据dogAge字段进行排序
     * @return
     */
    @ResponseBody
    @RequestMapping("/save")
    public List<Dog> testSave(){
        Dog dog = new Dog();
        dog.setDogNum(1005);//主键要加
        dog.setDogName("酥妮");
        dog.setDogAge(1);
        dogDao.save(dog);//保存新增
        List<Dog> dogList = dogDao.findAll();
        return dogList;
    }

}
